package com.wy.service.impl;

import com.wy.dao.TokenDao;
import com.wy.pojo.Token;
import com.wy.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * description TokenServiceImpl
 * author wy
 * date 2021-09-02 22:02
 **/
@Service
public class TokenServiceImpl implements TokenService
{

    @Autowired
    private TokenDao tokenDao;

    @Override
    public String getEmailByToken(String userToken)
    {
        Token token = new Token();
        token.setToken(userToken);
        Example<Token> example = Example.of(token);
        if (tokenDao.findOne(example).isPresent()){
            Token dbToken = tokenDao.findOne(example).get();
            return dbToken.getEmail();
        }
        return null;
    }

    @Override
    public void addToken(Token token)
    {
        tokenDao.save(token);
    }
}
